import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { QuestionsService } from './questions.service'

@Component({
  selector: 'app-questions',
  templateUrl: './questions.component.html',
  styleUrls: ['./questions.component.scss']
})
export class QuestionsComponent implements OnInit {

  @Input() qs:number;
  @Output()
  isokNext = new EventEmitter();

  data:any;
  currentAnswerNum:number=0;
 
  constructor(
    public questionsService: QuestionsService
  ) { }

  ngOnInit() {
    this.data = this.questionsService.questions[this.qs]
  }


  selectEle1(param,index) {
    if(param.completed) return;
    param.options.map((i, j) => {
      i.isSelected = j == index ? true : false;
    })
  }

  selectEle2(param,item) {
    if(param.completed) return;
    item.isSelected = !item.isSelected
  }

  snswerRight(paramitem) {
    if(paramitem.tipIsShow) return;
    paramitem.tip= "答对了";
    paramitem.completed= true;
    this.isokNext.emit(true);
  }


  answerError(paramitem) {
    this.currentAnswerNum++;
    if(this.currentAnswerNum>2){
      paramitem.tip= `当前正确答案：${paramitem.answerRight}`;
      paramitem.options.map((i,j) => {
        i.isSelected = i.isRight ? true : false;
      })
      paramitem.completed= true;
      paramitem.tipIsShow = true;
      this.isokNext.emit(true);
      return;
    }
    paramitem.tip= "答错了";
  }








  // 提交
  submit(paramitem) {
    let type=paramitem.type;
    switch(type) {
      case 1:
      let Right=paramitem.options.some((i, j) => {
        return i.isSelected && i.isRight ==1
     })
      if(Right) {
        this.snswerRight(paramitem)
      }else{
        this.answerError(paramitem)
      }
      break;
      case 2:
      let answernum=0;
      let answerlength=paramitem.answerRight.split(',').length;
      paramitem.options.map((i, j) => {
        if(i.isSelected && i.isRight ==1) {
        answernum++;
        }
      });
      if(answernum == answerlength) {
        this.snswerRight(paramitem)
      }else{
        this.answerError(paramitem)
      }
    } 
  }

}
